<template>
  <div class="index-login">
    <el-row :gutter="config.rowGutter">
      <el-col :xs="config.colSpanXs" :lg="config.colSpanLg">
        <el-form ref="loginFrom" :rules="rules" :model="loginFrom" :label-width="config.fromLabelWidth">
          <el-form-item :label="username.label" prop="username">
            <el-input v-model="loginFrom.username"></el-input>
          </el-form-item>
          <el-form-item :label="password.label" prop="password">
            <el-input type="password" v-model="loginFrom.password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit('loginFrom')">登录</el-button>
            <el-button @click="resetForm('loginFrom')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import returnCode from '../../const/returnCode'
  export default {
    name: 'indexLogin',
    data: function () {
      return {
        loginFrom: {
          username: '',
          password: ''
        },
        rules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' }
          ]
        },
        username: {
          label: '用户名',
          name: 'username',
          placeholder: '请输入用户名'
        },
        password: {
          label: '密码',
          name: 'password',
          placeholder: '请输入密码'
        },
        config: {
          colSpanXs: {span: 24},
          colSpanLg: {span: 12, offset: 6},
          rowGutter: 20,
          fromLabelWidth: '80px'
        }
      }
    },
    methods: {
      onSubmit: function (formName) {
        let _this = this
        this.$refs[formName].validate(function (valid) {
          if (valid) {
            _this.$http.post('/user/login', _this.loginFrom).then(res => {
              if (parseInt(res.data.status) === returnCode.success) {
                let info = res.data.result
                _this.$store.dispatch('user/login', info)
                _this.$store.dispatch('notification/success', {vue: _this, message: '登录成功'})
                _this.$router.push({
                  path: '/ownMsg/msgList'
                })
                console.log('login success', _this.$store.state.user)
              } else {
                _this.$store.dispatch('notification/warning', {vue: _this, message: '登录失败'})
                this.resetForm('loginFrom')
                console.log('login fail', res.data.result)
              }
            }).catch(res => {
              console.log(res)
            })
          } else {
            return false
          }
        })
      },
      resetForm: function (formName) {
        console.log('reset!')
        this.$http.get('/user').then(res => {
          console.log(res)
        })
        this.$refs[formName].resetFields()
      }
    }
  }
</script>
<style>
  .index-login{
    margin-top: 20px;
  }
</style>

